$(() => {
    function getList() {
        $.ajax({
            type: 'GET',
            url: BigNew.category_list,
            headers: {
                Authorization: localStorage.getItem('bigNews')
            },
            data: {},
            success: (res) => {
                console.log(res)
                const htmlStr = template('moban', res)
                $('tbody').html(htmlStr)

            },
        })
    }
    getList()
    let id = 0
    $('.btn_opt').on('click', function () {
        const name = $('#name').val().trim()
        const slug = $('#slug').val().trim()
        console.log(name, slug);
        $.ajax({
            type: 'POST',
            url: BigNew.category_add,
            headers: {
                Authorization: localStorage.getItem('bigNews')
            },
            data: { name, slug },
            success: (res) => {
                console.log(res)
            },
        })
        $('#name').val('')
        $('#slug').val('')
        $('#myModal').modal('hide')
        getList()

    })

    $("tbody").on("click", ".btn-info", function () {
        id = $(this).data().id
        $('#editModal').modal('show')
        $('#editModal #name').val($(this).data().name)
        $('#editModal #slug').val($(this).data().slug)
    })

    $('#editModal .btn-primary').on('click', function () {

        const name = $('#editModal #name').val()
        const slug = $('#editModal #slug').val()
        console.log(name, slug, id);
        $.ajax({
            type: 'POST',
            url: BigNew.category_edit,
            headers: {
                Authorization: localStorage.getItem('bigNews')
            },
            data: { id, name, slug },
            success: (res) => {
                console.log(res)
            },


        })
        $('#editModal').modal('hide')

    })

    $('tbody').on("click", ".btn-danger", function () {
        const id = $(this).data().id
        layer.confirm('是否删除?', { icon: 3, title: '温馨提示' }, (index) => {
            $.ajax({
                type: 'POST',
                url: BigNew.category_delete,
                headers: {
                    Authorization: localStorage.getItem('bigNews')
                },
                data: { id },
                success: (res) => {
                    getList()
                },
            })

            layer.close(index);
        });
    })


})